package com.example.cloudindex.web;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class MQAction {

    @Resource
    RabbitTemplate rabbitTemplate;

    ///////////////////////// 简单模式 //////////////////////////////
    // 消费者
    @RabbitListener(queuesToDeclare = @Queue("simple.queue"))
    public void oneToOne(String msg){
        System.out.println("msg = " + msg);
    }
    // 生产者
    @RequestMapping("sendOneToOne")
    public String sendOneToOne(String msg){
        rabbitTemplate.convertAndSend(
                "simple.queue",msg);
        return "一对一消息发送成功!";
    }

    ///////////////////////// 工作(竞争)模式 //////////////////////////////
    @RabbitListener(queuesToDeclare = @Queue("work.queue"))
    public void oneToMany1(String msg){
        System.out.println("oneToMany1 = " + msg);
    }
    @RabbitListener(queuesToDeclare = @Queue("work.queue"))
    public void oneToMany2(String msg){
        System.out.println("oneToMany2 = " + msg);
    }
    // 生产者
    @RequestMapping("sendOneToMany")
    public String sendOneToMany(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(
                    "work.queue",msg + "   " + i);
        }
        return "一对多消息发送成功!";
    }

    ///////////////////////// 广播订阅模式 //////////////////////////////
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "fanout",durable = "true"),
                   exchange = @Exchange(value="fanoutTest", type = ExchangeTypes.FANOUT))
    })
    public void fanout1(String msg){
        System.out.println("fanout1 = " + msg);
    }
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value="fanoutTest", type = ExchangeTypes.FANOUT))
    })
    public void fanout2(String msg){
        System.out.println("fanout2 = " + msg);
    }
    // 生产者
    @RequestMapping("sendFanout")
    public String sendFanout(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(
                    "fanoutTest","",msg + "   " + i);
        }
        return "广播订阅消息发送成功!";
    }


    ///////////////////////// 定向路由模式 //////////////////////////////
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "direct",durable = "true"),
                    exchange = @Exchange(value="directTest",
                            type = ExchangeTypes.DIRECT), key = {"info","error"})
    })
    public void direct1(String msg){
        System.out.println("direct1 = " + msg);
    }
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value="directTest",
                            type = ExchangeTypes.DIRECT), key = "error")
    })
    public void direct2(String msg){
        System.out.println("direct2 = " + msg);
    }
    // 生产者
    @RequestMapping("sendDirect1")
    public String sendDirect1(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(
                    "directTest","info",msg + "   " + i);
        }
        return "定向路由消息发送成功!";
    }

    @RequestMapping("sendDirect2")
    public String sendDirect2(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(
                    "directTest","error",msg + "   " + i);
        }
        return "定向路由消息发送成功!";
    }

    ///////////////////////// 主题模式 //////////////////////////////
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "topic",durable = "true"),
                    exchange = @Exchange(value="topicTest",
                            type = ExchangeTypes.TOPIC), key = {"user.*","order.#"})
    })
    public void topic1(String msg){
        System.out.println("topic1 = " + msg);
    }
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,
                    exchange = @Exchange(value="topicTest",
                            type = ExchangeTypes.TOPIC), key = {"user.#","order.*"})
    })
    public void topic2(String msg){
        System.out.println("topic2 = " + msg);
    }
    // 生产者
    @RequestMapping("sendTopic")
    public String sendTopic(String msg){
        rabbitTemplate.convertAndSend( "topicTest","user.insert","user.insert");
        rabbitTemplate.convertAndSend( "topicTest","order.update.save","order.update.save");
        rabbitTemplate.convertAndSend( "topicTest","user.update.delete","user.update.delete");
        rabbitTemplate.convertAndSend( "topicTest","user","user");
        rabbitTemplate.convertAndSend( "topicTest","order","order");
        rabbitTemplate.convertAndSend( "topicTest","user.create.top","user.create.top");
        rabbitTemplate.convertAndSend( "topicTest","order.remove.del","order.remove.del");
        return "主题路由消息发送成功!";
    }

}
